package com.qchistory;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ecbt.rest.util.DateUtil;
import com.opencsv.CSVParser;
import com.opencsv.CSVReader;

public class GenerateNewHis {

	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 10; i++) {
			Long time1 = new Date().getTime();
			File root = new File("D:\\projects\\工作目录\\版本\\7777-20240608\\SN_HIS_FILE");
			File[] files = root.listFiles();
			for (File file : files) {
//			System.out.println(file.getAbsolutePath() + "  " + file.getName() + " " + file.getParent());

				List<String> newCsvData = new ArrayList<>();
				String title = "VENDOR,SHIPPING_ID,RETURN_ID,SHT_ID,CUT_ID,PANEL_ID,TYPE,STATION,MACHINE,OPERATOR,MFG_DT,TXN_TIME,GRADE,RW,";
				newCsvData.add(title);

				List<String[]> csvData = readCSV(file.getAbsolutePath(), 1, "UTF-8");
				for (String[] data : csvData) {
					String one = printArrayData(data);
					newCsvData.add(one);
				}
				createCSV(file.getName(), newCsvData, "D:\\projects\\工作目录\\版本\\7777-20240608\\SN_NEW_FILE");
			}
			Long time2 = new Date().getTime();
			System.out.println((time2 - time1) / 1000.0);
			System.gc();
		}
	}

	public static String createCSV(String fileName, List<String> data, String path) {
		String filePath = path;

		File csvFile = null;
		BufferedWriter csvFileOutputStream = null;
		try {
			csvFile = new File(filePath + File.separator + fileName);
			File parent = csvFile.getParentFile();
			if (parent != null && !parent.exists()) {
				parent.mkdirs();
			}
			csvFile.createNewFile();
			// UTF-8使正确读取分隔符","
			csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"),
					1024);
			if (data != null) {
				for (String str : data) {
					csvFileOutputStream.write(str);
					csvFileOutputStream.write("\r\n");
				}
			}
			csvFileOutputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				csvFileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return path + File.separator + fileName;
	}

	private static String printArrayData(String[] data) {
		StringBuilder sb = new StringBuilder();
		int timeIndex = 0;
		for (String d : data) {
			if (timeIndex == 10) {
				String[] sd = businessDate(d);
				sb.append(sd[0] + ",");
				sb.append(sd[1] + ",");
			} else {
				sb.append(d + ",");
			}
			timeIndex++;
		}
		String res = sb.toString();
		return res.substring(0, res.length() - 1);
	}

	private static String[] businessDate(String d) {
		String sd = formatQcDate(d);
		return new String[] { DateUtil.changeToBusidate(sd), sd };
	}

	/**
	 * 20240101-011510 转换为 2024-01-01 01:15:10
	 * 
	 * @param d
	 * @return
	 */
	private static String formatQcDate(String d) {
		d = d.replace("-", " ");
		d = d.substring(0, 4) + "-" + d.substring(4, 6) + "-" + d.substring(6, 8) + " " + d.substring(9, 11) + ":"
				+ d.substring(11, 13) + ":" + d.substring(13, 15);
		return d;
	}

	public static List<String[]> readCSV(String fileName, int startLine, String gbcode) throws Exception {
		List<String[]> list = new ArrayList<String[]>();
		try {
			DataInputStream in = new DataInputStream(new FileInputStream(new File(fileName)));
			CSVReader csvReader = new CSVReader(new InputStreamReader(in, gbcode), CSVParser.DEFAULT_SEPARATOR,
					CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, startLine);
			String[] strs;
			while ((strs = csvReader.readNext()) != null) {
				list.add(strs);
			}
			csvReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
